<?php
    /*
     * Assign varibles		
     */
    $id = isset( $update_data_list['id'] ) ? $update_data_list['id'] : '';
    $title =  isset( $update_data_list['title'] ) ? $update_data_list['title'] : '';
    $image =  isset( $update_data_list['image'] ) ? $update_data_list['image'] : '';
    $description = isset( $update_data_list['description'] ) ? $update_data_list['description'] : '';
    $generic = isset( $update_data_list['generic'] ) ? $update_data_list['generic'] : '';
    $parent = isset( $update_data_list['parent_id'] ) ? $update_data_list['parent_id'] : 0;
    $is_parent = ( $parent  == 0 ) ? "Yes" : '';
    
    /* 
     * Update/Add data
     */
    if( $action == 'update' ||  $action == 'add' )
    {
        /*
         * Calling validation
         */
        echo App::Load("Helper/Javascript")->validation_map(array('_errBg'=>ERROR_BACKGROUND,'_dflBg'=>"#CECECE",'_autoSubmit'=>'true','_errorMark'=>'inline','_errToolTips'=>'false'));

        /*
         * HTML submit form start
         */
        echo $this->get_tag("form",array("action"=>$this->baseurl("/category/manage/{$type}/{$action}/{$id}"),"method"=>"post","enctype"=>'multipart/form-data',"class"=>"app_form app_validation","id"=>"auto_chk_submit"));
        
        /*
         * Call the tools bar
         */
        echo $this->callElement("default/tools/tool_bar",array("buttons"=>array(array("type"=>"save"),array("type"=>"save_and_update"),array("type"=>"file_manager"),array("type"=>"back") ) ,"title" => $definition['title']));

        /*
         * Grab The category tree informatin in to array
         */
        $cat_tree =array();
        $cat_tree2 =array();
        foreach($category_arr as $key => $val)
        {
            if( $id != $val['id'] )
            {
                $cat_tree[$val['id']] =$val['path_1'];
                $cat_tree2[$val['id']] =$val['path_2'];
            }
        }

        /*
         * Populate the hrml form fields
         */
        $Grid = App::Module('DataGrid') ->setDisplay('FormListing')
                                        ->addRow($definition['title'],App::load("Helper/Html")->inputTag("data[Category][title]",$title,array("id"=>"title","class"=>"app_input check_notempty")))
                                        ->addRow("",App::load("Helper/Html")->checkboxTag("data[Category][is_parent]",array("Yes" => "Parent"),$is_parent,array("id" => "is_parent")) . " " . App::load("Helper/Html")->selectTag("data[Category][parent_id]",$cat_tree2,$parent,array('id' =>"parent_id")));

        if($definition['description'] == 'Yes')
        {
            $Grid->addRow("Description",App::load("Helper/Html")->textareaTag("data[Category][description]",stripslashes($description),array("class"=>"app_input richtexteditor",'id'=>'note','rows'=> "20")));
        }

        if($definition['image'] == 'Yes')
        {
            $Grid->addRow("Image",App::load("Helper/Html")->imageTag("data[Category][image]",$image,array("id"=>"image","class"=>"app_input")),"Copy Image name from file manager");
        }
        
        $Grid->addRow("Sort Order",App::load("Helper/Html")->inputTag("data[Category][generic]",stripslashes($generic),array("class"=>"app_input",'id'=>'generic')));
        
        echo $this->callElement('default/tools/message', array('caption' =>'Message','type' => 'error', 'message' => $errors));
        $Grid->render();
        
        /*
         * Call the tools bar
         */
        echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array("type"=>"save"),array("type"=>"save_and_update"),array("type"=>"back")) ,"position" => 'bottom'));

        /*
         * HTML hidden fields
         */
        echo $this->get_tag("div",array("id"=>"hidden_fields"),
                App::load("Helper/Html")->hiddenTag("data[Category][type]",$type) .
                App::load("Helper/Html")->hiddenTag("data[Category][id]",$id) .
                $this->get_tag('br',array("class"=>"clearboth"))
            );
        echo $this->get_tag("/form");
        echo $this->get_tag("script",array("type"=>"text/javascript"),"            
            var apprain_category_manager ={

            parent_e			:	'parent_id',
            parents_toggoler	:	null,
            parient_selector	:	'is_parent',
            toggoler_fx			:	function()
                                    {
                                        if( $(apprain_category_manager.parent_e).getStyle('visibility') == 'visible' )
                                        {
                                            $(apprain_category_manager.parent_e).setStyle('visibility','hidden');
                                        }
                                        else 
                                        {
                                            $(apprain_category_manager.parent_e).setStyle('visibility','visible');
                                        }
                                    },
            init				:	function()
                                    {
                                        
                                        /* 
                                         * Check is the category parent it self	
                                         */
                                        if($(apprain_category_manager.parient_selector).checked == true)
                                        {
                                            $(apprain_category_manager.parent_e).setStyle('visibility','hidden');
                                        }
                                        
                                        /*
                                         * Bind event for parent selector
                                         */
                                        $(apprain_category_manager.parient_selector).addEvent('click', apprain_category_manager.toggoler_fx);
                                    }
        }
        window.addEvent('domready', apprain_category_manager.init);");
} 
else 
{ 
    /*
     * Top Tool bar
     */
    echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array('type'=>'back')) ,"title" => "{$definition['title']}"));

    
    /*
     * Display category area
     */
    foreach($category_arr as $key => $val)
    {
        $tmp = Array();
        $tmp[] = $this->get_tag('span',array('title'=>"Id:{$val['id']}"),(($data_list["page"] - 1) * $this->get_config('default_pagination') + $key + 1));

        if($definition['image'] == 'Yes')
        {
            $tmp[] = ($val['image'] !='')?App::load("Helper/Html")->imgDTag($this->get_img_dir($val['image']))	: '';
        }

        $tmp[] = $val['title'];        
        $tmp[] = $val[CATEGORY_PATH_MODE];
        
        if( app::__def()->sysConfig('ADMIN_REF_IN_CAT_INFO_SET') )
        {
            $tmp[] = App::Load("Module/AdminManager")->getAdminReferance($val);
        }
        
        $tmp[] = '<a href="' .  $this->baseurl('/')  . 'category/manage/' . $type .  '/update/' . $val['id'] . '"><img src="' . $this->baseurl('/') . '/images/admin/edit.gif" title="Edit ' . $val['title'] .  '" alt="Edit" /></a>' .
                        ' <a href="javascript:void(0)"  class="link_delete" title="Category_' . $val['id'] . '" >' . '<img src="' . $this->baseurl('/') . '/images/admin/remove.gif" title="Delete ' . $val['title'] .  '" alt="Delete" /></a>';
                        
        App::Call(App::Module('DataGrid'),'addRow',$tmp);
    }

    $t_head[] ="ID";
    if($definition['image'] == 'Yes')$t_head[] ="Image";
    $t_head[] =$definition['title'];
    $t_head[] ="Tree";
    if( app::__def()->sysConfig('ADMIN_REF_IN_CAT_INFO_SET') )
    {
        $t_head[] = "Admin Refernace";
    }
    $t_head[] ="Options";
    
    if(!empty($category_arr))
    {
        App::Module('DataGrid')->setHeader($t_head);
        App::Module('DataGrid')->setFooter( $data_list['link'] . " " . $data_list['paging'] . " &nbsp;&nbsp; Total Entry :" . $data_list["total"]);
		App::Module('DataGrid')->Render();
    }
    else
    {
        echo $this->callElement('default/tools/message', array('caption' =>'Message','type' => 'message', 'message' => 'No Data found.'));
    }

        echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array('type'=>'back')) ,"position" => 'bottom'));
}
